<template>
  <el-tag
    size="large"
    v-for="(item, index) in tags"
    :key="item.name"
    :closable="item.name !== 'home'"
    :disable-transitions="false"
    @close="handleClose(item, index)"
    @click="changeMenu(item)"
    :effect="$route.name === item.name ? 'dark' : 'plain'"
  >
    {{ item.meta.title }}
  </el-tag>
</template>
<script setup>
import { ref, nextTick } from 'vue'
import router from '../router/index'
import { useStore } from 'vuex'
const store = useStore()
/* if (JSON.parse(sessionStorage.getItem('tabsList'))) {
  store.state.tabsList = JSON.parse(sessionStorage.getItem('tabsList'))
} else {
  store.state.tabsList
} */
store.state.tabsList = JSON.parse(sessionStorage.getItem('tabsList'))

const tags = store.state.tabsList
// console.log(tags);

//选择标签跳转路由
const changeMenu = (tag) => {
  router.push({
    name: tag.name,
  })
}
// 关闭标签
const handleClose = (tag, index) => {
  let length = tags.length - 1
  store.commit('closeTab', tag)
  // 如果关闭的标签不是当前路由,就不跳转
  if (tag.name !== router.currentRoute.value.name) {
    return
  }
  // 关闭的标签时最右边的,往左边跳转一个
  if (index === length) {
    router.push({
      name: tags[index - 1].name,
    })
  } else {
    // 否则往右边跳转
    router.push({
      name: tags[index].name,
    })
  }
}
</script>
<style scoped>
.el-tag {
  margin: 15px 5px;
}
</style>